/*
 * console2.h
 *
 *  Created on: Dec 14, 2013
 *      Author: killerg
 */

#ifndef CONSOLE2_H_
#define CONSOLE2_H_


#include "share/cmd_handler.h"
#include "share/net_s.h"
#include "share/util.h"

#include <stdarg.h>
#include <set>
#include <sstream>


struct console_t2 {
    bool init_net();
    void destroy();
    void update();

    bool init_log(const char* name);
    bool start_accept(const char* ip, int port);
    int log(int mask, const char* fmt, ...);
    int vlog(int mask, const char* fmt, va_list args);
    bool reg(const char* cmd, const char* args, handle_t handler);


    net_s net;
    cmd_handler_t _processer;
    int fd;
    tran_s* _tran;
    std::set<tran_s*> _trans;
    bool _has_tran;
    bool _need_send;

    int _mask_f;
    int _mask_s;

    //---------------------------------------------------------------------
    typedef std::function<int()> int_cb_t;
    typedef std::function<double()> double_cb_t;

    void watch_n(const char* name, int_cb_t getcb, int_cb_t getmax = [](){return -1;}) {
        nwatchers.push_back(std::make_tuple(name, getcb, getmax));
    }
    void watch_f(const char* name, double_cb_t getcb, double_cb_t max = [](){return -1.0f;}) {
        fwatchers.push_back(std::make_tuple(name, getcb, max));
    }
    std::vector<std::tuple<std::string, int_cb_t, int_cb_t> > nwatchers;
    std::vector<std::tuple<std::string, double_cb_t, double_cb_t> > fwatchers;
    std::stringstream watch_ss;
    void update_watcher();
};

#endif /* CONSOLE2_H_ */
